package com.pda.yuanjing.pages.sycx;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CodeQueryBeanOut;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import com.public_module.tools.VoiceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QueryActivity extends BaseActivity<PageModel, PageView, PagePresent> implements PageView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.lll)
    LinearLayout lll;
    @BindView(R.id.listView)
    ListView listView;

    private String TAG = "QueryActivity";
    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    ItemAdapter itemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        ButterKnife.bind(this);
        tvTitle.setText("溯源查询 " + SharedPreferencesManger.getRealName());
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        String ss = StringProcess.codeMat(string);
                        presenter.query(ss);
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
    }

    @Override
    public PageModel createModel() {
        return new PageModelImpl(this);
    }

    @Override
    public PageView createView() {
        return this;
    }

    @Override
    public PagePresent createPresenter() {
        return new PagePresent();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
                listView.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void tipS(CodeQueryBeanOut codeQueryBeanOut) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                lll.removeAllViews();
                for (int i = 0; i < codeQueryBeanOut.getData().getCodeInfo().getListResult().size(); i++) {
                    LinearLayout layout = new LinearLayout(mContext);
                    layout.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    LP_MM.setMargins(10,10,10,0);
                    layout.setLayoutParams(LP_MM);
                    LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    TextView tv = new TextView(mContext);
                    tv.setText(codeQueryBeanOut.getData().getCodeInfo().getListResult().get(i).getKey());
                    tv.setTextSize(14);
                    tv.setTextColor(mContext.getColor(R.color.black));
                    tv.setLayoutParams(LP_WW);
                    layout.addView(tv);
                    TextView tv1 = new TextView(mContext);
                    tv1.setText(codeQueryBeanOut.getData().getCodeInfo().getListResult().get(i).getValue());
                    tv1.setTextSize(14);
                    tv1.setLayoutParams(LP_WW);
                    layout.addView(tv1);
                    lll.addView(layout);
                }
                listView.setVisibility(View.VISIBLE);
                itemAdapter = new ItemAdapter(QueryActivity.this, codeQueryBeanOut.getData().getListInfo());
                listView.setAdapter(itemAdapter);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                presenter.query(oet.getText().toString());
                break;
        }
    }
}
